// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © FunStriker

//@version=6
indicator("Ultimate Sclaping Indicator", overlay=true, timeframe="")


//--------------------------------------------------------
// SETTINGS
//--------------------------------------------------------
maLength = input.int(200, "Moving Average Length")
rsiLength = input.int(14, "RSI Length", minval=1)
rsiOB = input.float(70.0, "RSI Overbought Level", minval=50, maxval=100)
rsiOS = input.float(30.0, "RSI Oversold Level", minval=0, maxval=50)
rsiMid = 50.0
recencyBars = input.int(10, "Max Bars Since RSI Crossed Midline", minval=1)
showMA = input.bool(true, "Show 200 MA (5m)")
showVWAP = input.bool(true, "Show Session VWAP")

//--------------------------------------------------------
// INDICATORS
//--------------------------------------------------------

// 200 MA from 5m timeframe
ma200_5m = request.security(syminfo.tickerid, "5", ta.sma(close, maLength))

// Session VWAP
vwapSession = ta.vwap(hlc3)

// RSI
rsiValue = ta.rsi(close, rsiLength)

//--------------------------------------------------------
// RSI CROSS TIMING LOGIC
//--------------------------------------------------------

// Track when RSI last crossed above or below 50
rsiCrossAbove50 = ta.cross(rsiValue, rsiMid) and rsiValue > rsiMid
rsiCrossBelow50 = ta.cross(rsiValue, rsiMid) and rsiValue < rsiMid

// Store bar index of last crosses
var int lastAbove50Bar = na
var int lastBelow50Bar = na

if rsiCrossAbove50
    lastAbove50Bar := bar_index
if rsiCrossBelow50
    lastBelow50Bar := bar_index

// Calculate bars since each event
barsSinceAbove50 = bar_index - lastAbove50Bar
barsSinceBelow50 = bar_index - lastBelow50Bar

//--------------------------------------------------------
// CONDITIONS
//--------------------------------------------------------

// Core price conditions
priceAbove = close > ma200_5m and close > vwapSession
priceBelow = close < ma200_5m and close < vwapSession

// RSI zone conditions + recency filters
bullishRSI = rsiValue < rsiOS and barsSinceAbove50 <= recencyBars
bearishRSI = rsiValue > rsiOB and barsSinceBelow50 <= recencyBars

// Final trade signals
bullishCond = priceAbove and bullishRSI
bearishCond = priceBelow and bearishRSI

//--------------------------------------------------------
// PLOTS
//--------------------------------------------------------
plot(showMA ? ma200_5m : na, color=color.new(color.blue, 0), title="200 MA (5m)", linewidth=2)
plot(showVWAP ? vwapSession : na, color=color.new(color.orange, 0), title="Session VWAP", linewidth=2)

// Arrows
plotshape(bullishCond, title="Bullish Signal", style=shape.triangleup, color=color.new(color.lime, 0), size=size.small, location=location.belowbar)
plotshape(bearishCond, title="Bearish Signal", style=shape.triangledown, color=color.new(color.red, 0), size=size.small, location=location.abovebar)

//--------------------------------------------------------
// ALERTS
//--------------------------------------------------------
alertcondition(bullishCond, title="Bullish Confluence")
alertcondition(bearishCond, title="Bearish Confluence")